﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Lists;
using GeneralLibs;
using DevExpress.XtraCharts;

namespace BI
{
    public partial class UcBienDongTaiKhoan : UserControl
    {
        //DateTime _StartTime;
        //DateTime _FinishTime;
        //TimeSpan _TimeSpan;

        private string _Title = "";
        public string Title
        {
            set
            {
                _Title = value;
            }
            get
            {
                return _Title;
            }
        }

        public string TinhChatTaiKhoanMacDinh
        {
            set
            {
                try
                {
                    cmbTaiKhoan.EditValue = DanhMucCauHinhTaiKhoanMacDinh.Instance.GetTaiKhoanIDByTinhChat(value);
                }
                catch (Exception ex)
                {

                }
            }
        }


        public UcBienDongTaiKhoan()
        {
            InitializeComponent();
            //_StartTime = startTime;
            //_FinishTime = finishTime;
            //_TimeSpan = timeSpan;
        }

        private void UcBienDongTaiKhoan_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            cmbKyBaoCao.SelectedIndex = 17;
            numInterval.Value = 1;

            cmbTaiKhoan.DataSource = DanhMucTaiKhoan.Instance.DataView;
            cmbTaiKhoan.DisplayColumn = "SoHieu";
            cmbTaiKhoan.ValueColumn = "ID";
            cmbTaiKhoan.AddVisibleColumn("SoHieu", "Số hiệu", 100, 1);
            cmbTaiKhoan.AddVisibleColumn("Ten", "Tên", 300, 2);
            cmbTaiKhoan.EditValue = Guid.Empty;
        }

        private void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime ngayBatDau = dtNgayBatDau.DateTime;
                DateTime ngayKetThuc = dtNgayBatDau.DateTime;
                DateTime now = GeneralLibs.CommonLib.GetSystemDateTime();
                switch (cmbKyBaoCao.Text)
                {
                    case "Hôm nay":
                        dtNgayBatDau.Enabled = true;
                        dtNgayKetThuc.Enabled = true;
                        ngayBatDau = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
                        ngayKetThuc = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
                        break;
                    case "Tuần gần nhất":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0).AddDays(-7);
                        ngayKetThuc = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
                        break;
                    case "Tùy chọn":
                        dtNgayBatDau.Enabled = true;
                        dtNgayKetThuc.Enabled = true;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
                        break;
                    case "Cả năm":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 12, 31, 23, 59, 59);
                        break;
                    case "Quý I":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 3, 31, 23, 59, 59);
                        break;
                    case "Quý II":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 4, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 6, 30, 23, 59, 59);
                        break;
                    case "Quý III":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 7, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 9, 30, 23, 59, 59);
                        break;
                    case "Quý IV":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, 10, 1);
                        ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, 12, 31, 23, 59, 59);
                        break;
                    default:
                        try
                        {
                            dtNgayBatDau.Enabled = false;
                            dtNgayKetThuc.Enabled = false;
                            string strThang = ((string)cmbKyBaoCao.SelectedItem).Substring(((string)(cmbKyBaoCao.SelectedItem)).LastIndexOf(" "));
                            int thang = int.Parse(strThang);

                            ngayBatDau = new DateTime(GeneralLibs.Config.NamTaiChinh, thang, 1);
                            ngayKetThuc = new DateTime(GeneralLibs.Config.NamTaiChinh, thang,
                                            DateTime.DaysInMonth(GeneralLibs.Config.NamTaiChinh, thang), 23, 59, 59);
                        }
                        catch (SqlException sqlEx)
                        {
                            throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                        }
                        catch (Exception ex)
                        {
                            if (ex is CustomException) throw ex;
                            else
                            {
                                throw new Exception(ex.Message);
                            }
                        }
                        break;
                }
                dtNgayBatDau.DateTime = ngayBatDau;
                dtNgayKetThuc.DateTime = ngayKetThuc;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            BasicForms.FrmProgressBar.StartShowProgress();
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();

                DateTime dtPointNeedCalculate = dtNgayBatDau.DateTime;

                int total = (int)dtNgayKetThuc.DateTime.Subtract(dtNgayBatDau.DateTime).TotalHours / (int)numInterval.Value;
                int processed = 0;
                ucOneSeriesChart.Data.Clear();
                while (dtPointNeedCalculate <= dtNgayKetThuc.DateTime)
                {
                    SoDuTaiKhoan soDu = Lists.SoDuTaiKhoan.CalculateFromDB(cmd, dtPointNeedCalculate, (Guid)cmbTaiKhoan.EditValue);
                    DataRow row = ucOneSeriesChart.Data.NewRow();
                    row["Series1Name"] = string.Format("{0}/{1}/{2}", dtPointNeedCalculate.Day, dtPointNeedCalculate.Month, dtPointNeedCalculate.Year);
                    row["Series1Value"] = Math.Abs(soDu.DuNo - soDu.DuCo)/1000000;
                    ucOneSeriesChart.Data.Rows.Add(row);
                    processed++;

                    BasicForms.FrmProgressBar.Status = string.Format("{0}/{1}", processed, total);
                    dtPointNeedCalculate = dtPointNeedCalculate.AddDays((double)numInterval.Value);
                }

                ucOneSeriesChart.GenChart("Số dư", ViewType.Area, _Title + " (ĐV: triệu VNĐ)");
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
            }

            BasicForms.FrmProgressBar.FinishShowProgress();
        }
    }
}
